﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleProcedureRepo
    {
        public const string m_msg_head = "Title Result";

        public IEnumerable<TitleProcedureModels> GetTitleProcedure()
        {
            List<TitleProcedureModels> TitleProcedures = new List<TitleProcedureModels>();
            string SQLCommand = @"select * from TBTitleProcedure WHERE Status <> 'Delete' order by TitleProcedureId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleProcedureModels TitleProcedure = new TitleProcedureModels();
                        TitleProcedure.TitleProcedureId = (int)reader["TitleProcedureId"];
                        TitleProcedure.TitleProcedureName = (string)reader["TitleProcedureName"];
                        TitleProcedure.Status = (string)reader["Status"];

                        TitleProcedures.Add(TitleProcedure);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleProcedures.ToList();
        }

        public TitleProcedureModels GetTitleProcedureById(int id)
        {
            TitleProcedureModels TitleProcedure = new TitleProcedureModels();
            string SQLCommand = @"select * from TBTitleProcedure where TitleProcedureId=@TitleProcedureId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleProcedureId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleProcedure.TitleProcedureId = (int)reader["TitleProcedureId"];
                        TitleProcedure.TitleProcedureName = (string)reader["TitleProcedureName"];
                        TitleProcedure.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleProcedure;
        }

        public bool Create(TitleProcedureModels TitleProcedure)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleProcedure
                                    values (@TitleProcedureName,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleProcedureName", TitleProcedure.TitleProcedureName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleProcedureModels TitleProcedure)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleProcedure 
                                    Set TitleProcedureName=@TitleProcedureName
                                    where TitleProcedureId=@TitleProcedureId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleProcedureId", TitleProcedure.TitleProcedureId);
                    db.AddParameter("@TitleProcedureName", TitleProcedure.TitleProcedureName);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleProcedureModels TitleProcedure)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleProcedure
                                    Set Status=@Status
                                    where TitleProcedureId=@TitleProcedureId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleProcedureId", TitleProcedure.TitleProcedureId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}